﻿using ConfExpert.Forms.Base;

using DevTeam;

using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class AksesuarRezervelerim : XForm
    {
        public AksesuarRezervelerim()
        {
            InitializeComponent();
        }

        private void AksesuarCikisTalepleri_Load(object sender, EventArgs e)
        {

            gvRezerve.Init("GetAksesuarRezerveByUser", WhoAmI, chkKapaliRezerve.Checked);
            gvRezerve.AddDetailGrid(gvRezerveLines, "AksesuarRezerveId");

            gvRezerve.AddColumn("ToplamRezerve", typeof(int), "SUM(Child({FK}).RezerveMiktari)");

            gvRezerve.FormatAsNumber(0, "ToplamRezerve");
            gvRezerveLines.FormatAsNumber(0, "RezerveMiktari", "TalebeDonusen", "KalanRezerve");

            gvRezerveLines.AddSummary("RezerveMiktari");
            gvRezerve.AddSummary("ToplamRezerve");

            gvRezerveLines.MakeColumnInvisible("AksesuarRezerveId", "AksesuarRezerveLinesId");

            GridManager gm = GridManager.GetManager(gvRezerve);
            gm.GridMenu.AddMenu("Rezerveyi İptal Et", closeReserve);

            gm = GridManager.GetManager(gvRezerveLines);
            gm.GridMenu.AddMenu("Seçilen aksesuarın rezervesini çöz", UnboundReservation);


            BestFitColumnsForThisForm();
            chkKapaliRezerve.CheckedChanged += chkKapaliRezerve_CheckedChanged;
            splitContainerControl1.AutoSize();
            btnUpdate.ItemClick += btnUpdate_ItemClick;
        }

        private void UnboundReservation(object sender, EventArgs e)
        {
            int AksesuarRezerveLinesId = gvRezerveLines.GetFocusedRowCellValue("AksesuarRezerveLinesId").ToInt();
            Aksesuar.AksesuarRezerveCoz ac = new Aksesuar.AksesuarRezerveCoz(AksesuarRezerveLinesId);
            DialogResult dr = ac.ShowDialog();
            if (dr == DialogResult.OK)
            {
                btnUpdate.PerformClick();
            }
        }

        void chkKapaliRezerve_CheckedChanged(object sender, EventArgs e)
        {
            btnUpdate.PerformClick();
        }

        void closeReserve(object sender, EventArgs e)
        {
            DataRow dr = gvRezerve.GetDataRow(gvRezerve.FocusedRowHandle);
            int handle = gvRezerve.FocusedRowHandle;
            if (handle > -1)
            {
                int stGenelId = dr["stGenelId"].ToInt();
                if (stGenelId == 1)
                {
                    int RezerveId = intParse(dr["AksesuarRezerveId"]);
                    ExecuteNonQuery("CloseAksesuarRezerve", RezerveId);
                    btnUpdate.PerformClick();
                }
            }
        }

        private void BestFitColumnsForThisForm()
        {
            gvRezerve.SetCaptions("AksesuarRezerveId:RzvNo");
            //gvRezerveLines.OptionsView.EnableAppearanceOddRow = false;
            //gvRezerveLines.OptionsView.EnableAppearanceEvenRow = false;  
            gvRezerve.BestFitColumns();
            gvRezerveLines.BestFitColumns();
            gvRezerve.Columns["Notlar"].Width = 100;
        }

        void btnUpdate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvRezerve.EndEditing();
            gvRezerve.RefreshDataSource(true, "GetAksesuarRezerveByUser", WhoAmI, chkKapaliRezerve.Checked);
            BestFitColumnsForThisForm();
        }
    }
}